package com.cloud.cloudvideo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.cloudvideo.entity.SysPermission;
import com.cloud.cloudvideo.entity.TreePojo;
import com.cloud.cloudvideo.mapper.SysPermissionMapper;
import com.cloud.cloudvideo.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {

	
	@Autowired
	private SysPermissionMapper permessionMapper;
	
	@Override
	public List<SysPermission> list() {
		
		return permessionMapper.selectByExample(null);
	}

	@Override
	public void modifyPermission(SysPermission permission) {
		if(null == permission.getId()) {
			permission.setCreateBy("1");
			permission.setCreateTime(new Date());
			permessionMapper.insertSelective(permission);
		}else {
			permission.setUpdateTime(new Date());
			permessionMapper.updateByPrimaryKeySelective(permission);
		}
			
	}

	@Override
	public List<TreePojo> treelist() {
		List<TreePojo> list = permessionMapper.findPermissionWithChild();
		TreePojo sp = new TreePojo();
		sp.setId(-1L);
		sp.setParentId(-1L);
		sp.setTitle("顶级菜单");
		sp.setChildren(list);
		List<TreePojo> l = new ArrayList<TreePojo>();
		l.add(sp);
		return l;
	}

	@Override
	public Map<String,Object> menuInfo(Integer id) {
		Map<String,Object> map = new HashMap<>();
		SysPermission menu = permessionMapper.selectByPrimaryKey(id.longValue());
		SysPermission pmenu = permessionMapper.selectByPrimaryKey(menu.getParentId());
		map.put("menu", menu);
		map.put("pmenu", pmenu);
		return map;
	}

}
